package com.jian.sys.controller;

import com.jian.sys.common.CacheBean;
import com.jian.sys.common.CachePool;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/cache")
public class CacheController {
    public static volatile Map<String,Object> CACHE_CONTAINER=CachePool.CACHE_CONTAINER;
    /**
     * 加载所有的缓存
     * @return
     */
    @RequestMapping("/loadAllCache")
    public DataGridView loadAllCache(){
        List<CacheBean> cacheBeanList=new ArrayList<>();
        Set<Map.Entry<String, Object>> entrySet=CACHE_CONTAINER.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            cacheBeanList.add(new CacheBean(entry.getKey(),entry.getValue()));
        }
        return new DataGridView(cacheBeanList);
    }
    /**
     * 同步缓存
     */
    @RequestMapping("/syncCache")
    public ResultObj syncCache(){
        CachePool.syncCache();
        return ResultObj.OPERATE_SUCCESS;
    }
    /**
     * 同步缓存
     */
    @RequestMapping("/removeAllCache")
    public ResultObj removeAllCache(){
        CachePool.removeAll();
        return ResultObj.OPERATE_SUCCESS;
    }
    /**
     * 删除缓存
     */
    @RequestMapping("/removeCacheByKey")
    public ResultObj removeCacheByKey(String key){
        CachePool.removeCacheByKey(key);
        return ResultObj.DELETE_SUCCESS;
    }
}
